import { _decorator, Component, Node, Label } from 'cc';
import {Timer} from "db://oops-framework/core/common/timer/Timer";

const { ccclass, property } = _decorator;

// 方向灯
@ccclass('WheelView')
export class WheelView extends Component {
    @property({
        type: Node,
        tooltip: "东"
    })
    dong: Node

    @property({
        type: Node,
        tooltip: "南"
    })
    nan: Node

    @property({
        type: Node,
        tooltip: "西"
    })
    xi: Node

    @property({
        type: Node,
        tooltip: "北"
    })
    bei: Node

    @property({
        type: Label,
        tooltip: "倒计时"
    })
    counter: Label

    // 玩家的方向
    playerDirect: {}
    // 倒计时间
    waitTime: number
    private timer: Timer
    isStart: boolean
    onLoad() {
        // 1秒定时器
        this.timer = new Timer(1)
        this.isStart = false
        this.playerDirect = {}
    }

    startCounter(shortId: number, waitTime: number) {
        this.waitTime = waitTime
        this.isStart = true
        for (const k of Object.keys(this.playerDirect)) {
            // 只倒计时当前的玩家
            this.playerDirect[k].active = Number(k) === shortId;
        }
        this.counter.string = waitTime.toString()
    }

    // 记录玩家所在方向
    bindCounter(shortId: number, pos: number) {
        this.playerDirect[shortId] = [this.dong, this.nan, this.xi, this.bei][pos]
    }

    stop() {
        this.timer.stop()
        this.isStart = false
        this.waitTime = 0
    }

    // 重置方向
    resetPlayer() {
        this.playerDirect = {}
    }

    update(dt: number) {
        if (!this.isStart) {
            return
        }
        if (this.timer.update(dt)) {
            // 有一秒了
            if (this.waitTime > 0) {
                this.waitTime--
                this.counter.string = this.waitTime.toString()
            } else {
                // 可以了
                // TODO 自动出牌，同时托管
                console.log("定时器到时了,用户还没出牌")
            }
        }
    }
}
